import { createRouter, createWebHistory } from 'vue-router'
import { ref } from 'vue'
import { userStore } from '@/stores/user'
const router = createRouter({
  history: createWebHistory(import.meta.env.BASE_URL),
  routes: [
    {
      path: '/Login',
      name: 'login',
      component: () => import('@/views/Login/UserLogin.vue')
    },
    {
      path: "/404",
      component: () => import('@/views/Error/404.vue'),
    },
    {
      path: '/public',
      name: 'public',
      component: () => import('@/views/Public/PublicIndex.vue'),
    },
    {
      path: '/',
      name: 'home',
      component: () => import('@/views/Layout/LayoutIndex.vue'),
      redirect: '/index',
      children: [
        {
          path: 'index',
          component: () => import('@/views/Index/HomeIndex.vue')
        },
        {
          path: 'PersonalForms',
          component: () => import('@/views/PersonalForms/PersonalFormsIndex.vue')
        },
        {
          path: 'timeManager',
          component: () => import('@/views/TimeManager/TimeManagerIndex.vue')
        },
        {
          path: 'UserManager',
          component: () => import('@/views/UserManager/UserManagerIndex.vue')
        },
        {
          path: 'Exam',
          children:[
            {
              path: 'BookExam',
              component: () => import('@/views/Exam/BookExam/BookExamIndex.vue')
            },
            {
              path: 'ExamManager',
              component: () => import('@/views/Exam/ExamManager/ExamManagerIndex.vue')
            },
          ]
        },
        {
          path: 'Interview',
          children:[
            {
              path: 'BookInterview',
              component: () => import('@/views/Interview/BookInterview/BookInterviewIndex.vue')
            },
            {
              path: 'InterviewManager',
              component: () => import('@/views/Interview/InterviewManager/InterviewManagerIndex.vue')
            },
          ]
        },
        {
          path: 'Class',
          children:[
            {
              path: 'Classinfo',
              component: () => import('@/views/Class/Classinfo/ClassinfoIndex.vue')
            },
            {
              path: 'ClassinfoManager',
              component: () => import('@/views/Class/ClassinfoManager/ClassinfoManagerIndex.vue')
            },
            {
              path: 'ClassMembers',
              component: () => import('@/views/Class/ClassMembers/ClassMembersIndex.vue')
            },
            {
              path: 'ClassMembersManager',
              component: () => import('@/views/Class/ClassMembersManager/ClassMembersManagerIndex.vue')
            },
          ]
        },
        {
          path: 'Group',
          children:[
            {
              path: 'GroupsManager',
              component: () => import('@/views/Group/GroupsManager/GroupsManagerIndex.vue'),
            },
            {
              path: 'GroupMembersManager',
              component: () => import('@/views/Group/GroupMembersManager/GroupMembersManagerIndex.vue')
            },
            {
              path: 'Groups',
              component: () => import('@/views/Group/Groups/GroupsIndex.vue')
            },
            {
              path: 'GroupsMembers',
              component: () => import('@/views/Group/GroupMembers/GroupMembersIndex.vue')
            },
            {
              path: 'GroupSelect',
              component: () => import('@/views/Group/GroupSelect/GroupSelectIndex.vue')
            },
            {
              path: 'NotGroupMembersManager',
              component: () => import('@/views/Group/NotGroupMembersManager/NotGroupMembersManagerIndex.vue')
            }
          ]
        },
        {
          path: 'Config',
          children:[
            {
              path: 'ConfigManager',
              component: () => import('@/views/Config/ConfigManager/ConfigManagerIndex.vue')
            }
          ]
        }
      ]
    }
  ]
})
router.beforeEach((to, from, next) => {
  if (to.path === '/Login') return next()
  if (to.path === '/index') return next()
  if (to.path === '/404') return next()
  const DroleList = ref<string[]>([])
  DroleList.value =userStore().roleList
  if (DroleList.value.length === 0) return next('/Login')
  else{
    if(to.path.includes('Manager')){
      if(DroleList.value.includes('admin')) return next()
      else return next('/404')
    }
    else return next()
  }
})

export default router
